#This file houses the command line interpriter.
import System
from parse import Parser #load parser.
from errors import * #load programming errors.

#the interpreter.
class cmd:
 def __init__(self, stack, parser): #requires instances of  the stack class and parser class.
  #store passed variables.
  self.parser=parser
  self.stack=stack

 #start interpreting.
 def start(self):
  print "Type quit to quit."#welcome message, needs improvement.
  line=System.Console.ReadLine() #get a line of text.
  while line != "quit": #start the main loop.
   if len(line) > 0:
    while line[-1] == "|": #allow user to join lines with | 
     System.Console.Write("Excepted:     ") #prompt for the next line.
     line=line[:-1]+" "+System.Console.ReadLine() #get the next line and join to what has already been read, removing the | from the end of the last text.
    #try to parse the text.
    try: 
     self.parser.ParseLine(line)
    except CompilingErrorException as e: #except programming errors.
     print e.message #tell the user about the message.
   self.status() #print status.
   line=System.Console.ReadLine() #get the next line and do it all again.

 #Print the last entry on the stack and write "ok:", in other words, prompt the user.
 def status(self):
  if self.stack.count() > 0: #if something is on the stack,
   System.Console.Write(self.stack.peak()) #peak at the stack and print its top value.
  else: #empty stack.
   System.Console.Write("empty. ") #say so.
  print ", ok:     " #prompt